package maiyatian.videolive.adapter.main;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import maiyatian.videolive.IClickListener;
import maiyatian.videolive.MainActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.adapter.ViewHolder;
import maiyatian.videolive.bean.LiveBean;

/**
 * Created by 王中阳 on 2016/5/10.
 */
public class hotAdapter extends BaseAdapter {
    private MainActivity mycontext;
    private List<LiveBean> datas;
    private IClickListener mCallBack; //信息回调

    public hotAdapter(MainActivity mycontext, List<LiveBean> datas,IClickListener mCallBack) {
        this.datas = datas;
        this.mycontext = mycontext;
        this.mCallBack = mCallBack;
    }

    public void setDatas(List<LiveBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (datas != null) {
            return datas.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LiveBean bean = null;
        bean = datas.get(position);
        if (convertView == null) {
            convertView = mycontext.getLayoutInflater().inflate(R.layout.item_hot, parent, false);
            AutoUtils.autoSize(convertView);
        }
        TextView liveStyle = ViewHolder.get(convertView, R.id.liveStyle);
        TextView onLookNum = ViewHolder.get(convertView, R.id.onLookNum);
        TextView nick = ViewHolder.get(convertView, R.id.nick);
        TextView location = ViewHolder.get(convertView, R.id.location);
        TextView introLive = ViewHolder.get(convertView, R.id.introLive);
        TextView topic = ViewHolder.get(convertView, R.id.topic);
        RelativeLayout liveBg = ViewHolder.get(convertView, R.id.liveBg);
        ImageView avator = ViewHolder.get(convertView, R.id.avator);
        liveBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onItemClicked(position);
            }
        });
        if (bean != null) {
            if (bean.isLive()) {
                liveStyle.setText(mycontext.getResources().getString(R.string.live));
            } else {
                liveStyle.setText(mycontext.getResources().getString(R.string.liveOver));
            }
            onLookNum.setText(bean.getLookNum() + mycontext.getResources().getString(R.string.onLook));
            introLive.setText(bean.getLiveIntro());
            topic.setText("#" + bean.getTopic() + "#");
            if (bean.getUserBean() != null) {
                nick.setText(bean.getUserBean().getNick());
                location.setText(bean.getUserBean().getLoation());
            }
        }
        return convertView;
    }
}
